import java.awt.Graphics2D;  
import java.awt.Image;  
import java.awt.Toolkit;  
import java.awt.image.BufferedImage;  
import java.awt.image.ImageProducer;  
import java.awt.image.MemoryImageSource;  
import java.awt.image.*;
import java.io.File;  
import java.io.FileInputStream;
import javax.imageio.ImageIO;

import imagereader.IImageIO;


public class ImplementImageIO implements IImageIO{
    private Image image;
    private int tempOfInt = 0xff;
    

    @Override
    public Image myRead(String pathname){
        // TODO Auto-generated method stub
        try {
            File file = new File(pathname);
            FileInputStream fins = new FileInputStream(file);
            
            // 位图头
            byte bmpFileHeader[] = new byte[14];
            // 位图信息
            byte bmpInfoHeader[] = new byte[40];
            
            fins.read(bmpFileHeader, 0, 14);
            fins.read(bmpInfoHeader, 0, 40);
            
            // 保存位图宽度（以像素个数表示）
            int biWidth = (int) ( (bmpInfoHeader[7] & tempOfInt) << 24 
					| (bmpInfoHeader[6] & tempOfInt) << 16
					| (bmpInfoHeader[5] & tempOfInt) << 8 
					| (bmpInfoHeader[4] & tempOfInt) );
            
            // 保存位图高度（以像素个数表示）
            int biHeight = (int) ( (bmpInfoHeader[11] & tempOfInt) << 24 
					| (bmpInfoHeader[10] & tempOfInt) << 16
					| (bmpInfoHeader[9] & tempOfInt) << 8 
					| (bmpInfoHeader[8] & tempOfInt) );
            
            // 保存每个像素的位数，它是图像的颜色深度。常用值是1、4、8（灰阶）和24（彩色）
            int biBitCount = (int) ( (bmpInfoHeader[15] & tempOfInt) << 8 
					| (bmpInfoHeader[14] & tempOfInt));

            
            // 保存图像大小
            int biSizeImage = (int) ( (bmpInfoHeader[23] & tempOfInt) << 24 
					| (bmpInfoHeader[22] & tempOfInt) << 16
					| (bmpInfoHeader[21] & tempOfInt) << 8 
					| (bmpInfoHeader[20] & tempOfInt) );
            
            // 当biBitCount=24时,1个像素占3个字节;
            if(biBitCount == 24) {
                int numOfEmptyByte = biSizeImage / biHeight - 3 * biWidth;
                
                if (numOfEmptyByte == 4) {
                    numOfEmptyByte = 0;
                }
                int temp = 0;

                int pixel[] = new int [biWidth * biHeight];
                
                // 位图全部像素占用的字节数
                byte allBmpByte[] = new byte[biSizeImage];
                fins.read(allBmpByte, 0, biSizeImage);
                
                // 扫描行之间是从下到上,扫描行内是从左到右
                for(int i = biHeight - 1; i >= 0; i--) {
                    for(int j = 0; j < biWidth; j++) {
                        pixel[i * biWidth + j] =  (tempOfInt << 24 
                                | (allBmpByte[temp+2] & tempOfInt) << 16
                                | (allBmpByte[temp+1] & tempOfInt) << 8 
                                | (allBmpByte[temp] & tempOfInt));
                        temp += 3;
                    }
                    temp += numOfEmptyByte;
                }
                image = Toolkit.getDefaultToolkit().createImage(
                        (ImageProducer)new MemoryImageSource(biWidth, biHeight, pixel, 0, biWidth));
            }
            
            fins.close();
            return image;
            
        } catch (Exception e) {
            // TODO: handle exception
            return (Image)null;   
        }
    }

    @Override
    public Image myWrite(Image image, String pathname){
        // TODO Auto-generated method stub
        try {
            File file = new File(pathname + ".bmp");
            BufferedImage bufferedImage = new BufferedImage(
                    image.getWidth((ImageObserver)null), image.getHeight((ImageObserver)null), BufferedImage.TYPE_INT_RGB);
            Graphics2D graph = bufferedImage.createGraphics();
            graph.drawImage(image, 0, 0, null);
            graph.dispose();
            ImageIO.write(bufferedImage, "bmp", file);
            return image;
        } catch (Exception e) {
            // TODO: handle exception
            return (Image)null;
        }
        
    }
    
}